<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Withdrawals;
use think\Cookie;

class User extends Api
{

    public function index()
    {
        $toekn = request()->post('token');
        $user_id = $this->getUserId($toekn);
        if ($user_id == 300) {
            return $this->responseSend(0, '', 300);
        }
        $user_info = model('Users')->where('id', $user_id['user_id'])->find();
        if (!$user_info) {
            return $this->responseSend(0, '', 300);
        }
        $user_arr = $user_info->getData();
        return $this->responseSend($user_arr['id'], $user_arr, 200);
    }

    public function update()
    {
        $data = request()->post();
        $user_id = $this->getUserId($data['token']);
        if ($user_id == 300) {
            return $this->responseSend(0, '', 300);
        }
        if (!isset($data['password'])) {
            return $this->responseSend($user_id['user_id'], '请填写密码', 160);
        }
        model('Users')->where('id', $user_id['user_id'])->update(['password' => password_hash($data['password'], PASSWORD_DEFAULT)]);

    }

    public function reg()
    {
        //session_id('sl91hijc57nca73cv1gnn3ej3b');
        $data = request()->post();
        $s_info = db('code')->where('id', $data['id'])->find();
        $s_id = $s_info['session_id'];
        session_id($s_id);
        db('code')->where('id', $data['id'])->delete();
        if (strlen($data['password']) < 6) {
           return $this->responseSend(0, '密码不能少于6位', 160);
        }
        $index = new Index();
        $r = $index->check_verify($data['code'], $data['id']);
        if (isset($data['id']) && $r) {
            $model = model('Users');
            $info = $model->where('username', $data['username'])->find();
            if ($info) {
                return $this->responseSend(0, '账号已注册', 160);
            }
            $insert_data = [
                'username' => $data['username'],
                'password' => password_hash($data['password'], PASSWORD_DEFAULT),
                'money' => 0.00,
                'status' => 1,
                'create_at' => time(),
                'update_at' => time(),
            ];
            $model->save($insert_data);
            return $this->responseSend($model->id, '注册成功', 200);
        } else {
            return $this->responseSend(0, '验证码错误', 160);
        }
    }

    public function car()
    {
        $data = request()->post();
        $user_id = $this->getUserId($data['token']);
        if ($user_id == 300) {
            return $this->responseSend(0, '', 300);
        }
        $info = db('ShopCar')
            ->alias('a')
            ->field('a.number,b.name,b.goods_id')
            ->join('sku b', 'a.sku_id=b.id', 'left')
            ->where('a.user_id', $user_id['user_id'])
            ->select();
        return $this->responseSend($user_id['user_id'], $info, 200);
    }

    public function changeCar()
    {
        $data = request()->post();
        $user_id = $this->getUserId($data['token']);
        if ($user_id == 300) {
            return $this->responseSend(0, '', 300);
        }
        $update = [
            'number' => $data['number'],
        ];
        if (isset($data['id']) && $data['id']) {
            db('ShopCar')->where('id', $data['id'])->update($update);
        } else {
            $update['user_id'] = $user_id['user_id'];
            $update['sku_id'] = $data['sku_id'];
            db('ShopCar')->insert($update);
        }
        return $this->responseSend($user_id['user_id'], '', 200);
    }

    public function delCar()
    {
        $data = request()->post();
        $user_id = $this->getUserId($data['token']);
        if ($user_id == 300) {
            return $this->responseSend(0, '', 300);
        }
        if (isset($data['id']) && $data['id']) {
            db('ShopCar')->where('id', $data['id'])->delete();
        } else {
            db('ShopCar')->where('user_id', $user_id['user_id'])->delete();
        }
        return $this->responseSend($user_id['user_id'], '', 200);
    }

}
